package com.example.fw;

import static com.example.fw.ContactHelper.CREATION;
import static com.example.fw.ContactHelper.MODIFICATION;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.tests.ContactData;
import com.example.utils.ListOf;
import com.example.utils.SortedListOf;

public class ContactHelper extends HelperBase {
	
	public static boolean CREATION = true;
	public static boolean MODIFICATION = false;

	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}

	private SortedListOf<ContactData> cachedContacts;
	private ListOf<ContactData> cachedNotSortContacts; 
	
	
	public SortedListOf<ContactData> getContacts() {
		if (cachedContacts == null) {
			rebuildCache();
		}
		return cachedContacts;
	}
	
	/*  public ListOf<ContactData> getNotSortContacts() { //!!!!!!!!!
	public ListOf<ContactData> getContacts() {
		if (cachedNotSortContacts == null) {
			//rebuildNotSortCache();
			rebuildCache();
		}
		return cachedNotSortContacts;
	}
	 */
	
	private void rebuildCache() {
	cachedContacts = new SortedListOf<ContactData>();
		
		manager.navigateTo().mainPage();
		List<WebElement> rows = driver.findElements(By.xpath("//tr[@name='entry']"));
		for (WebElement row : rows) {
			List<WebElement> cells = row.findElements(By.tagName("td")); 
			String contactname = cells.get(2).getText();  
			String lastname =cells.get(1).getText();
			cachedContacts.add(new ContactData()
				.withContactname(contactname)
				.withLastname(lastname));
		}
	}
	
	public ContactData getContactDataByIndex(int index) {
		manager.navigateTo().mainPage();
		WebElement contactElement = driver.findElement(By.xpath("(//tr[@name='entry'])[" + (index+1) + "]"));
		List<WebElement> cells = contactElement.findElements(By.tagName("td"));
		String contactname = cells.get(2).getText(); 
		String lastname =cells.get(1).getText();
		return new ContactData().withContactname(contactname).withLastname(lastname);
	}
	
	/*private void rebuildNotSortCache() { //!!!!!!!!!!!
		cachedNotSortContacts = new ListOf<ContactData>();
			
			manager.navigateTo().mainPage();
			List<WebElement> rows = driver.findElements(By.xpath("//tr[@name='entry']"));
			for (WebElement row : rows) {
				List<WebElement> cells = row.findElements(By.tagName("td")); 
				String contactname = cells.get(2).getText();  
				String lastname =cells.get(1).getText();
				cachedNotSortContacts.add(new ContactData()
					.withContactname(contactname)
					.withLastname(lastname));
			}
		}
*/

	public ContactHelper createContact(ContactData contact) {
		manager.navigateTo().mainPage();
		initContactCreation();
		fillContactForm(contact, CREATION);
		submitContactCreation();
		returnToHomePage();
		rebuildCache();
		return this;
		
	}

	public ContactHelper deleteContact(int index) {	
		   selectContactByIndex(index);
		   submitContactRemoval();
		   returnToHomePage();
		 //  rebuildNotSortCache(); //!!!!!!!!!
		   rebuildCache();
		   return this;
		}


	public void submitContactRemoval() {
		click(By.xpath("(//input[@value='Delete'])"));
		cachedContacts = null;
	}
	
	public ContactHelper modifyContact(int index, ContactData contact,
			boolean MODIFICATION2) {
		selectContactByIndex(index);
		fillContactForm(contact, MODIFICATION);
		submitContactModification();
		returnToHomePage();
		rebuildCache();
		return this;
	}
	
	
	//--------------------------------------------------------------------
	
	public ContactHelper submitContactCreation() {
		click(By.name("submit"));
		cachedContacts = null;
		return this;
	  }

	public ContactHelper returnToHomePage() {
		click(By.linkText("home page"));
		 return this;
	  }

	public ContactHelper fillContactForm(ContactData contact, boolean formType) {
		type(By.name("firstname"), contact.getContactname());
		type(By.name("lastname"), contact.getLastname());
		type(By.name("address"), contact.getAddress());
		type(By.name("home"), contact.getHome());
		type(By.name("mobile"), contact.getMobile());
		type(By.name("work"), contact.getWork());
		type(By.name("email"), contact.getEmail());
		type(By.name("email2"), contact.getEmail2());
		selectByText(By.name("bday"), contact.getBday());
		selectByText(By.name("bmonth"), contact.getBmonth());
		type(By.name("byear"), contact.getByear());
	    if (formType == CREATION) {
	    	//selectByText(By.name("new_group"), "[none]");
	    } else {	
	      if (driver.findElements(By.name("new_group")).size() != 0) {
	    	  throw new Error("Group selector exists in contact modification form");
	      }
	    }
		type(By.name("address2"), contact.getAddress2());
		type(By.name("phone2"), contact.getHome2());
		 return this;
		}
	
	public ContactHelper initContactCreation() {
		manager.navigateTo().mainPage();
	    click(By.linkText("add new"));
	    return this;
	  }


	public ContactHelper selectContactByIndex(int index) {
		click(By.xpath("(//img[@alt='Edit'])[" + (index+1) + "]"));
		 return this;
	}
	
	

	public ContactHelper submitContactModification() {
		click(By.xpath("(//input[@value='Update'])"));	
		cachedContacts = null;
		return this;
	}
	
	}
	